<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-10-30
 * Time: 11:13:04
 * Info:
 */

namespace plugin\book\app\controller\admin;

use app\common\behavior\response;
use support\Request;
use plugin\book\app\model\Book as BookModel;
use plugin\book\app\model\Doc as DocModel;

class DocController
{

    /**
     * 不需要鉴权的方法
     * @var string[]
     */
    public $noNeedAuthAction = ['doc_tree'];

    public function index(Request $request)
    {
        if ($request->isPost()) {
            $book_id = $request->post('book_id');
            $count   = DocModel::count();
            $data    = DocModel::order('sort asc,id asc')->where(['book_id' => $book_id])->select()->toArray();

            return json(['code' => 0, 'count' => $count, 'data' => $data, 'msg' => 'ok']);
        }

        $book_id = $request->get('book_id');
        if (empty($book_id)) {
            return response('参数错误');
        }
        $detail = BookModel::where(['id' => $book_id])->find();
        if (empty($detail)) {
            return response('获取数据失败');
        }

        return view('admin/doc/index', ['detail' => $detail]);
    }

    public function add(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->post();
            if (empty($param['title'])) {
                return json(['code' => 0, 'msg' => '标题不能为空']);
            }
            $param['admin_id'] = cmf_get_admin_id();

            $res = DocModel::create($param);

            return json(['code' => 200, 'msg' => '操作成功', 'data' => $res->id]);
        }

        $book_id = $request->get('book_id');
        $pid     = $request->get('pid', 0);

        return view('admin/doc/add', ['book_id' => $book_id, 'pid' => $pid]);
    }

    public function edit(Request $request)
    {
        if ($request->isPost()) {
            $param = $request->all();
            DocModel::strict(false)->field(true)->update($param);

            return success_json('保存成功');
        }
        $param = $request->all();
        $id    = empty($param['id']) ? 0 : $param['id'];
        $pid   = empty($param['pid']) ? 0 : $param['pid'];

        if ($id > 0) {
            $detail            = DocModel::where(['id' => $id])->find();
            $detail['content'] = htmlspecialchars_decode($detail['content']);
        }

        return view('admin/doc/doc_add', ['id' => $id, 'pid' => $pid, 'detail' => $detail]);
    }

    public function delete(Request $request)
    {
        $id = $request->all();
        $id = $id['id'] ?? 0;
        if (empty($id)) {
            return error_json('参数错误');
        }
        $count = DocModel::where(["pid" => $id, "status" => 1])->count();
        if ($count > 0) {
            return error_json('该分类下还有子文档，无法删除');
        }
        DocModel::where('id', $id)->delete();

        return success_json('删除成功');
    }

    public function doc_tree(Request $request)
    {
        $param   = $request->all();
        $where[] = ['status', '>', 0];
        $where[] = ['book_id', '=', $param['id']];
        $list    = DocModel::where($where)->field('id,pid,title,type,book_id,sort')->order('sort asc,id asc')->select();
        foreach ($list as $k => &$v) {
            $v['title'] = sub_str($v['title'], 10);
        }
        $tree          = get_tree($list, 0, 4);
        $data['trees'] = $tree;

        return json($data);
    }

}
